﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.ComponentModel;
using System.ComponentModel.Design;
using AGuang.WebTool;

[assembly: WebResource("AGuang.Script.AGuang_Client.Controls.Upload.js", "text/javascript")]
[assembly: WebResource("AGuang.Script.AGuang_Client.css.Upload.css", "text/css", PerformSubstitution = true)]
[assembly: WebResource("AGuang.Script.AGuang_Client.css.images.Upload.ok.jpg", "image/jpeg")]
[assembly: WebResource("AGuang.Script.AGuang_Client.css.images.Upload.error.jpg", "image/jpeg")]
[assembly: WebResource("AGuang.Script.AGuang_Client.css.images.Upload.ajax-loader.gif", "image/gif")]
[assembly: WebResource("AGuang.Script.AGuang_Client.css.images.Upload.closeButton.gif", "image/gif")]
namespace AGuang.Script {
    public class Upload:BaseScript {
        private String forTextBox;
        private String activeButton;
        private String action;
        private bool autoScript;
        private String[] fileTypes;
        private String uploadedHanlder;

        protected override void OnLoad(EventArgs e) {
            base.OnLoad(e);
            ClientScriptManager csm = Page.ClientScript;
            AddScriptContorl(new Action());
            AddStyle(csm.GetWebResourceUrl(this.GetType(), "AGuang.Script.AGuang_Client.css.Upload.css"));
            AddScript(csm.GetWebResourceUrl(this.GetType(), "AGuang.Script.AGuang_Client.Controls.Upload.js"));
        }

        protected override void OnPreRender(EventArgs e) {
            if (!AutoScript) return;
            if (String.IsNullOrEmpty(ForTextBox)) throw new Exception("ForTextBox必须设定.");
            if (String.IsNullOrEmpty(Action)) throw new Exception("Action必须设定.");
            StringBuilder sb = new StringBuilder();
            Control c = this.Parent.FindControl(ForTextBox);
            if (c != null) { ForTextBox = c.ClientID; }
            if (String.IsNullOrEmpty(ActiveButton)) {
                sb.AppendFormat("var {0}=new Page.Upload('{1}');", JsVar, ForTextBox);
            } else {
                Control c2 = this.Parent.FindControl(ActiveButton);
                if (c2 != null) { ActiveButton = c2.ClientID; }
                sb.AppendFormat("var {0}=new Page.Upload('{1}','{2}');", JsVar, ForTextBox, ActiveButton);
            }
            sb.AppendFormat("{0}.SetAction('{1}');", JsVar, Action);
            if (FileTypes != null && FileTypes.Length > 0) {
                sb.AppendFormat("{0}.SetFileTypes({1});", JsVar, JSON.Object2Json(FileTypes));
            }
            if (!String.IsNullOrEmpty(UploadedHandler)) {
                sb.AppendFormat("{0}.Add_UploadedHanld({1});", JsVar, UploadedHandler);
            }
            AddEffect(sb.ToString());
        }

        public String ForTextBox {
            get { return forTextBox; }
            set { forTextBox = value; }
        }

        public String ActiveButton {
            get { return activeButton; }
            set { activeButton = value; }
        }

        public String Action {
            get { return action; }
            set { action = value; }
        }

        public String UploadedHandler {
            get { return uploadedHanlder; }
            set { uploadedHanlder = value; }
        }

        [TypeConverter(typeof(TypeConvert.ArrayConverter))]
        public String[] FileTypes {
            get { return fileTypes; }
            set { fileTypes = value; }
        }

        /// <summary>
        /// 是否自动生成脚本
        /// </summary>
        public bool AutoScript {
            get { return autoScript; }
            set { autoScript = true; }
        }

        public String JsVar {
            get { return "s" + this.ClientID; }
        }


    }
}
